<script setup lang="ts">
import CustomNavBar from './components/CustomNavBar/CustomNavBar.vue'
import { getCategoryHeadAPI, getHomeBannerAPI, getHotAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import CategoryPanel from '@/components/CategoryPanel.vue'
import HotPanel from '@/components/HotPanel.vue'
import XtxGuess from '@/components/XtxGuess.vue'
import type { XtxGuessInstance } from '@/types/component'
import PageSkeleton from '@/pages/index/components/PageSkeleton.vue'

const BannerList = ref<BannerItem[]>([])
const CategoryHeadList = ref<CategoryItem[]>([])
const HotList = ref<HotItem[]>([])
const guessRef = ref<XtxGuessInstance>()

const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  // console.log(res)
  BannerList.value = res.result
}
const getCategoryHeadData = async () => {
  const res = await getCategoryHeadAPI()
  CategoryHeadList.value = res.result
  // console.log(CategoryHeadList.value)
}

const getHotData = async () => {
  const res = await getHotAPI()
  // console.log(res)
  HotList.value = res.result
}
const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getCategoryHeadData(), getHotData()])

  isLoading.value = false
})
//下拉触底调用猜你喜欢组件（XtxGuess）中获取数据的方法
const toLower = () => {
  guessRef.value?.getMore()
  console.log('daodilaaaaaaaaaaaaaaaaaaaaa')
}
const refreshTag = ref(false)
const refresh = async () => {
  refreshTag.value = true
  // await getHomeBannerData()
  // await getCategoryHeadData()
  // await getHotData()
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getCategoryHeadData(),
    getHotData(),
    guessRef.value?.getMore(),
  ])
  refreshTag.value = false
}
</script>

<template>
  <CustomNavBar />
  <scroll-view
    :refresher-triggered="refreshTag"
    class="scroll"
    scroll-y
    @scrolltolower="toLower"
    @refresherrefresh="refresh"
    refresher-enabled
  >
    <PageSkeleton v-if="isLoading" />
    <template>
      <XtxSwiper :list="BannerList" />
      <CategoryPanel :list="CategoryHeadList" />
      <HotPanel :list="HotList" />
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll {
  flex: 1;
}
</style>
